package grpc

import (
	"context"
	"go.uber.org/zap"
	"google.golang.org/grpc/codes"
	"google.golang.org/grpc/status"
	"jk-time/microBook/pkg/limiter"
)

type LimitServer struct {
	limiter limiter.Limiter
	UserServiceServer
}

func (l *LimitServer) GetByID(ctx context.Context, in *GetByIDRequest) (*GetByIDResponse, error) {
	limited, err := l.limiter.Limit(ctx, "getById")
	if err != nil {
		zap.L().Error("判定限流出现问题", zap.Error(err))
		return &GetByIDResponse{}, status.Errorf(codes.ResourceExhausted, "触发限流")
	}
	if limited {
		return &GetByIDResponse{}, status.Errorf(codes.ResourceExhausted, "触发限流")
	}
	return l.UserServiceServer.GetByID(ctx, in)
}
